import 'package:flutter/material.dart';
import 'package:flutter_cupertino_date_picker/flutter_cupertino_date_picker.dart';

class ThirdDatePickerDemo extends StatefulWidget {
  ThirdDatePickerDemo({Key key}) : super(key: key);

  @override
  _ThirdDatePickerDemoState createState() => _ThirdDatePickerDemoState();
}

class _ThirdDatePickerDemoState extends State<ThirdDatePickerDemo> {
  DateTime _dateTime = DateTime.now();
  
  void _showDatePicker() {
    DatePicker.showDatePicker(
      context,
      onMonthChangeStartWithFirstDate: true,
      pickerTheme: DateTimePickerTheme(
        showTitle: true,
        confirm: Text('确定', style: TextStyle(color: Colors.red)),
        cancel: Text('取消', style: TextStyle(color: Colors.yellow)),
      ),
      minDateTime: DateTime.parse("2010-05-12"),
      maxDateTime: DateTime.parse("2021-11-25"),
      initialDateTime: _dateTime,
      dateFormat: "yyyy-MMMM-dd",
      locale: DateTimePickerLocale.zh_cn,
      onClose: () => print("----- onClose -----"),
      onCancel: () => debugPrint('onCancel'),
      // onChange: (dateTime, List<int> index) {
      //   setState(() {
      //     _dateTime = dateTime;
      //   });
      // },
      onConfirm: (dateTime, List<int> index) {
        setState(() {
          _dateTime = dateTime;
        });
      },
    );
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("第三方日期选择器"),
      ),
      body: Container(
       child: InkWell(
         child: Row(
           children: <Widget>[
             Text("打开日期选择器"),
             Icon(Icons.ac_unit),
             Text("${_dateTime}")
           ],
         ),
         onTap: _showDatePicker
       )
      )
    );
  }
}